package com.itzhoun.createModel.singleton;

/**
 * @author 周楠
 * @Date 2022/12/11/下午 2:40
 */
public class SingletonLazySync {
    public SingletonLazySync() {
    }

    private static SingletonLazySync singletonLazySync;

    //第一种
//    public static synchronized SingletonLazySync getInstance(){
//        if (singletonLazySync == null){
//            singletonLazySync = new SingletonLazySync();
//            System.out.println("加锁同步:" + singletonLazySync);
//        }
//        return singletonLazySync;
//    }

    //第二种
//    public static SingletonLazySync getInstance(){
//        if (singletonLazySync == null){
//            synchronized (SingletonLazy.class){
//                singletonLazySync = new SingletonLazySync();
//            }
//        }
//        return singletonLazySync;
//    }

    //第三种 双重校验锁
    public static SingletonLazySync getInstance(){
        if (singletonLazySync == null){
            synchronized (SingletonLazySync.class){
                if (singletonLazySync == null){
                    singletonLazySync = new SingletonLazySync();
                }
            }
        }
        return singletonLazySync;
    }


    public static void main(String[] args) {
        Runnable runnable = SingletonLazySync::getInstance;

        Thread thread = new Thread(runnable);
        thread.start();
        Thread thread2 = new Thread(runnable);
        thread2.start();
    }
}
